/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ver_datos.java
 *
 * Created on 4/07/2011, 06:58:47 PM
 */
package curd;


import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;

/**
 *
 * @author Javier
 */
public class Ver_datos extends javax.swing.JFrame {

    /** Creates new form Ver_datos */
    public Ver_datos() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        t_consulta = new javax.swing.JTable();
        boton_listar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        t_consulta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id Empleado", "Nombre", "Apellido", "Dirección", "Estado Civil", "Puesto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        t_consulta.setCellSelectionEnabled(true);
        t_consulta.setName("t_consulta"); // NOI18N
        jScrollPane1.setViewportView(t_consulta);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(curd.CURDApp.class).getContext().getResourceMap(Ver_datos.class);
        t_consulta.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title0")); // NOI18N
        t_consulta.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title1")); // NOI18N
        t_consulta.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title2")); // NOI18N
        t_consulta.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title3")); // NOI18N
        t_consulta.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title4")); // NOI18N
        t_consulta.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("t_consulta.columnModel.title5")); // NOI18N

        boton_listar.setText(resourceMap.getString("boton_listar.text")); // NOI18N
        boton_listar.setName("boton_listar"); // NOI18N
        boton_listar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_listarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(393, Short.MAX_VALUE)
                .addComponent(boton_listar)
                .addGap(72, 72, 72))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(boton_listar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boton_listarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_listarActionPerformed
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();        
            Query q = session.createQuery("from Cjempleado");
            Vector tableData = new Vector();
            List lista = q.list();
            Vector<String> tableHeaders = new Vector<String>();
            tableHeaders.add("Id empleado"); 
            tableHeaders.add("Nombres");
            tableHeaders.add("Apellidos");
            tableHeaders.add("Dirección");
            //tableHeaders.add("Puesto");
            tableHeaders.add("Estado Civil");
            Vector<Object> oneRow = null;
            for(Object o : lista) 
            {
                Cjempleado empleado = new Cjempleado((Cjempleado)o);
                 oneRow = new Vector<Object>();
                oneRow.add(empleado.getIdEmpleado());
                oneRow.add(empleado.getNombres());
                oneRow.add(empleado.getApellidos());
                oneRow.add(empleado.getDireccion());
                //oneRow.add(empleado.getCjpuesto());
                oneRow.add(empleado.getEstadoCivil());
                tableData.add(oneRow);
            }
            t_consulta.setModel(new DefaultTableModel(tableData, tableHeaders));
            session.getTransaction().commit();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e.toString());
        }
    
        
    }//GEN-LAST:event_boton_listarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ver_datos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_listar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable t_consulta;
    // End of variables declaration//GEN-END:variables
}
